package com.wmwsystems.inc.demo.wsserver;

import com.wmwsystems.service.flights.ArrayOfAirCompany;
import com.wmwsystems.service.flights.PullAirCompaniesRequest;
import com.wmwsystems.service.flights.PullAirCompaniesResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
import org.springframework.ws.server.endpoint.annotation.RequestPayload;
import org.springframework.ws.server.endpoint.annotation.ResponsePayload;

@Endpoint
public class FlightsEndpoint {

    private static final String NAMESPACE_URI = "http://wmwsystems.com/service/flights";

    private FlightsRepository flightsRepository;

    @Autowired
    public  FlightsEndpoint(FlightsRepository flightsRepository) {
        this.flightsRepository = flightsRepository;
    }

    @PayloadRoot(namespace = NAMESPACE_URI, localPart = "pullAirCompaniesRequest")
    @ResponsePayload
    public PullAirCompaniesResponse pullAirCompanies(@RequestPayload PullAirCompaniesRequest pullAirCompaniesRequest) {
        PullAirCompaniesResponse response = new PullAirCompaniesResponse();

        final ArrayOfAirCompany arrayOfAirCompany = new ArrayOfAirCompany();
        arrayOfAirCompany.getAirCompany().addAll(flightsRepository.getAirCompanies());
        response.setAirCompanies(arrayOfAirCompany);

        response.setVersion(pullAirCompaniesRequest.getVersion() + 1);

        return response;
    }

}
